package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.DeviceViewHolder;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.model.LoginRecordResponse;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;

import java.util.ArrayList;
import java.util.List;

public class DeviceActivity extends BaseActivity {

    RecyclerView rv_device;
    DeviceAdapter adapter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_device;
    }

    @Override
    public void findView() {
        setTitle("登录设备");
        rv_device = findViewById(R.id.rv_device);
        rv_device.setNestedScrollingEnabled(false);
        rv_device.setFocusable(false);
        rv_device.setLayoutManager(new LinearLayoutManager(DeviceActivity.this));
        rv_device.addItemDecoration(new LinearLayouDecoration(DeviceActivity.this,
                LinearLayouDecoration.HORIZONTAL, R.dimen.border8, R.color.transparent));
        rv_device.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new DeviceAdapter(this);
        rv_device.setAdapter(adapter);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void afterCreate() {
        post(Url.listLoginRecord, "", LoginRecordResponse.class, new FFNetWorkCallBack<LoginRecordResponse>() {
            @Override
            public void onSuccess(LoginRecordResponse response) {
                if (response.getCode() == 200) {
                    adapter.setData(response.getData());
                }
            }

            @Override
            public boolean onFail(LoginRecordResponse response) {
                return false;
            }
        }, "{\"deviceId\":\"" + getIMEI() + "\"}");

    }

    @SuppressLint("HardwareIds")
    public String getIMEI() {
        String id;
//        //android.telephony.TelephonyManager
        TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null) {
            id = mTelephony.getDeviceId();
        } else {
            //android.provider.Settings;
            id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return id;
    }

    public class DeviceAdapter extends BaseRecyclerAdapter<DeviceViewHolder> {
        private Context context;


        private List<LoginRecordResponse.DataBean> mData = new ArrayList<>();


        public DeviceAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<LoginRecordResponse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<LoginRecordResponse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public DeviceViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.device_item, parent, false);
            DeviceViewHolder viewHolder_text = new DeviceViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public DeviceViewHolder getViewHolder(View view) {
            return new DeviceViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(final DeviceViewHolder holder, @SuppressLint("RecyclerView") final int position, boolean isItem) {
            holder.tv_name.setText(mData.get(position).getDeviceName());
            holder.tv_time.setText("登录时间：" + FFUtils.dealDateFormat(mData.get(position).getCreateTime()));
            holder.tv_ip.setText("登录IP：" + mData.get(position).getIp());
            if (position == 0) {
                holder.tv_select.setVisibility(View.VISIBLE);
            } else {
                holder.tv_select.setVisibility(View.GONE);
            }

        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }
}
